$(function(){
		//var i = '${i}';//计数器
		
		//如果没有文件则显示提示信息
		if(getAllFolderAndFile() == 0){
			//alert(".no-result-file-download show");
			$(".no-result-file-download").show();
		}
		
		//全选事件
		 $("#selectedAll").click(function(){
			$("input[name='selectedFolder']").prop("checked",$(this).is(":checked"));
			$("input[name='selectedFolder']").nextAll(".rename").show();
			$("input[name='selectedFile']").prop("checked",$(this).is(":checked"));
			$("select[name='viewPermissions']").show();
			$("input[name='selectedFile']").nextAll(".rename").show();
		});	
		//文件夾复选框选中事件	
		$("input[name='selectedFolder']").click(function(){
			$("#selectedAll").prop("checked",getAllFolderAndFile() == getAllSelectedFolderOrFile()?true:false);
			$(this).nextAll(".rename").show();
			$(this).nextAll("select[name='viewPermissions']").show();
		});
		//文件复选框选中事件	
		$("input[name='selectedFile']").click(function(){
			$("#selectedAll").prop("checked",getAllFolderAndFile() == getAllSelectedFolderOrFile()?true:false);
		});
		
		//鼠标经过
		$(".item").hover(function(){
			$(this).children().css("background","#eff8fe");
			$(this).children().first().children(".viewPermissions").show();
			$(this).children().first().children(".rename").show();
		},function(){
			if(!$(this).children().first().children().first().is(':checked')){
				$(this).children().first().children(".viewPermissions").hide();
				$(this).children().first().children(".rename").hide();
				$(this).children().css("background","#fff");
			}
		});
		
		//没有文件，点击图片上传按钮
		$("#noResultUpload").click(function(){
			$("input[name='upload']").click();
		});
		
		//上传文件事件
		$("input[name='upload']").change(function(){
			if(checkUpload() && uploadFileCheck(this)){
				$("#myForm").submit();
			}
		});
		//创建文件夹
			$("#createFolder").click(function(){
				var newRow = createRow();
				//当前页面存在文件夹则在其末尾处添加，否则新建一个
				if($("input[name='folderId']").length > 0){
					$("input[name='folderId']").last().parent().parent().after(newRow);
				}
				else{
						if($(".headDiv").children("list-cols") == 0){
							var header = createHeader();
							$(".headDiv").after(header); 
						}
						$(".no-result-file-download").before(newRow);
						$(".no-result-file-download").hide();
						updateAllFileNum(1);
						$("#allFileNum").html("已全部加载，共"+initFileNum+"个");
				}
				$("#newFolder"+i+"").focus();
				
				/*if(i > 0){
					$("input[name^='newFolder']").last().val("新建文件夹("+i+")");
				}*/
				//使顶部按钮失效
				forbitTopBtn(true);
				$("#allFileNum").html("已全部加载，共"+initFileNum+"个");
				//点击确定，保存文件
				$("#insure"+i+"").click(function(){
					var defaultFolderName = validateFolderName($(this).prev().val());
					$("input[name^='newFolder']").last().val(defaultFolderName);
					createFolder($("input[name^='newFolder']").last().val(),"#insure"+i+"");
				});
				//点击取消
				$("#cancel"+i+"").click(function(){
					$(this).parent().parent().remove();
					$(".title-left").attr("disabled", false);
					//使顶部按钮生效
					forbitTopBtn(false);
					updateAllFileNum(-1);
					$("#allFileNum").html("已全部加载，共"+initFileNum+"个");
					$(".no-result-file-download").show();
				});
			});
		
			//下载文件
			$("#download").click(function(){
				if(checkSelectedFolderOrFile()){
					var downloadFileArray = [];
					$("input[name='selectedFile']:checked").each(function(){
						var tmpJson = {"fileId":$(this).next().val()};
						downloadFileArray.push(tmpJson); 
					});
					var strDowanload = JSON.stringify(downloadFileArray);
					console.log("strDowanload="+strDowanload);
					$("#selectedDownload").val(strDowanload);
					var action = getBasePath()+"/FileAction_downloadFile.action";
					$("#myForm").attr("action",action);
					$("#myForm").submit();
				}
			});
			
			//权限修改
			$("#editPermissions").click(function(){
				if(checkSelectedFolderOrFile()){
					var selectedFileArray = [];
					var selectedFolderArray = [];
					$("input[name='selectedFolder']:checked").each(function(){
						var tmpJson = {"folderId":$(this).next().val(),"viewPermissions":$(this).nextAll(".viewPermissions").val()};
						selectedFolderArray.push(tmpJson); 
					});
					$("input[name='selectedFile']:checked").each(function(){
						var tmpJson = {"fileId":$(this).next().val(),"viewPermissions":$(this).nextAll(".viewPermissions").val()};
						selectedFileArray.push(tmpJson); 
					});
					console.log("selectedFolderArray="+JSON.stringify(selectedFolderArray));
					console.log("selectedFileArray="+JSON.stringify(selectedFileArray));
					editPermissions(selectedFolderArray,selectedFileArray);
				}
			});
			
			//删除文件或文件夹
			$("#delete").click(function(){
				if(checkSelectedFolderOrFile()){
					var deleteFolderArray = [];
					var deleteFileArray = [];
					$("input[name='selectedFolder']:checked").each(function(){
						var tmpJson = {"folderId":$(this).next().val()};
						deleteFolderArray.push(tmpJson); 
					});
					$("input[name='selectedFile']:checked").each(function(){
						var tmpJson = {"fileId":$(this).next().val()};
						deleteFileArray.push(tmpJson); 
					});
					console.log("deleteFolderArray="+JSON.stringify(deleteFolderArray));
					console.log("deleteFileArray="+JSON.stringify(deleteFileArray));
					deleteFolderOrFile(deleteFolderArray,deleteFileArray);
				}
			});
			
			
			
			
			/*//选中图片
			$("img[name='fileImage']").each(function(){
				var imageType = getImageType();
				var documentType = getDocumentType();
				var musicType = getMusicTypeType();
				var thisFileType = $(this).prev().val();
				var location = (window.location+'').split('/');
				var basePath = location[0]+'//'+location[2]+'/'+location[3];
				if(imageType.toString().indexOf(thisFileType) > -1){
					$(this).attr("src",basePath+"/images/image.png");
				}
				else if(musicType.toString().indexOf(thisFileType) > -1){
					$(this).attr("src",basePath+"/images/music.png");
				}
				else{
					$(this).attr("src",basePath+"/images/file.png");
				}
			});*/
			
			//选中浏览设置
			$("select[name='setViewPermissions'] option").each(function(){
				if($(this).val() == $(this).parent().attr("value")){
					$(this).attr("selected",true);
				}
			});
			
			//还原文件或文件夹
			$("#restore").click(function(){
				var len = $("input[name='selectedFolder']:checked").length + $("input[name='selectedFile']:checked").length;
				if(len <= 0){
					alert("至少选择一个文件夹或文件！");
				}
				else{
					var restoreFolderArray = [];
					var restoreFileArray = [];
					$("input[name='selectedFolder']:checked").each(function(){
						var tmpJson = {"folderId":$(this).next().val()};
						restoreFolderArray.push(tmpJson); 
					});
					$("input[name='selectedFile']:checked").each(function(){
						var tmpJson = {"fileId":$(this).next().val()};
						restoreFileArray.push(tmpJson); 
					});
					console.log("restoreFolderArray="+JSON.stringify(restoreFolderArray));
					console.log("restoreFileArray="+JSON.stringify(restoreFileArray));
					restoreFolderOrFile(restoreFolderArray, restoreFileArray);
				}
			});
			
			//删除文件或文件夹
			$("#deleteRealFile").click(function(){
				var len = $("input[name='selectedFolder']:checked").length + $("input[name='selectedFile']:checked").length;
				if(len <= 0){
					alert("至少选择一个文件夹或文件！");
				}
				else{
					var deleteFolderArray = [];
					var deleteFileArray = [];
					$("input[name='selectedFolder']:checked").each(function(){
						var tmpJson = {"folderId":$(this).next().val()};
						deleteFolderArray.push(tmpJson); 
					});
					$("input[name='selectedFile']:checked").each(function(){
						var tmpJson = {"fileId":$(this).next().val()};
						deleteFileArray.push(tmpJson); 
					});
					console.log("deleteFolderArray="+JSON.stringify(deleteFolderArray));
					console.log("deleteFileArray="+JSON.stringify(deleteFileArray));
					deleteRealFile(deleteFolderArray,deleteFileArray);
				}
			});
		
			
			//选择用户全选事件
			 $("#selectedAll").click(function(){
				$("input[name='selectedUser']").prop("checked",$(this).is(":checked"));
			});	
			//用户复选框选中事件	
			$("input[name='selectedUser']").click(function(){
				var selectedLen = $("input[name='selectedUser']:checked").length;
				var totalLen =  $("input[name='selectedUser']").length;
				$("#selectedAll").prop("checked",selectedLen == totalLen?true:false);
			});
			
			//添加好友事件
			$("#addFriends").click(function(){
				if(checkUser()){
					var addFriendsArray = [];
					$("input[name='selectedUser']:checked").each(function(){
						var tmpJson = {
												"userId":$(this).nextAll("input[name='hiddenUserId']").val(),
										};
						addFriendsArray.push(tmpJson); 
						console.log("addFriendsArray="+JSON.stringify(addFriendsArray));
					});
					addFriends(addFriendsArray);
				}
			});
			
			//删除好友事件
			$("#deleteFriends").click(function(){
				if(checkUser()){
					var deleteFriendsArray = [];
					$("input[name='selectedUser']:checked").each(function(){
						var tmpJson = {
												"userId":$(this).nextAll("input[name='hiddenUserId']").val(),
										};
						deleteFriendsArray.push(tmpJson); 
						console.log("deleteFriendsArray="+JSON.stringify(deleteFriendsArray));
					});
					deleteFriends(deleteFriendsArray);
				}
			});
			
			
			//确认添加好友事件
			$("#confirmAddFriends").click(function(){
				if(checkUser()){
					var confirmAddFriendsArray = [];
					$("input[name='selectedUser']:checked").each(function(){
						var tmpJson = {
												"userId":$(this).nextAll("input[name='hiddenUserId']").val(),
										};
						confirmAddFriendsArray.push(tmpJson); 
						console.log("confirmAddFriendsArray="+JSON.stringify(confirmAddFriendsArray));
					});
					confirmAddFriends(confirmAddFriendsArray);
				}
			});
			
		/*	//点击我的消息
			$('#topFrame', window.parent.document).contents().find("#newMessage").click(function(){
				$('#topFrame', window.parent.document).contents().find("#newMessage").hide();
				showFlag = 0;
				console.log("showFlag = 0;");
			});*/
			
		//没有文件时点击图片上传
		$("#no-result_upload").click(function(){
			$("input[name='upload']").click();
		});
		
		//点击审核事件
		$("#aKeyAudit").click(function(){
			var result = $("select[name='approvedResult'] option:selected").val();
			var approvedArray = [];
			if(checkUser()){
				$("input[name='selectedUser']:checked").each(function(){
					var tmpJson = {
											"id":$(this).nextAll("input[name='id']").val(),
											"userId":$(this).nextAll("input[name='userId']").val(),
											"password":$(this).nextAll("input[name='password']").val(),
											"nickName":$(this).nextAll("input[name='nickName']").val(),
											"avatar":$(this).nextAll("input[name='avatar']").val(),
											"sex":$(this).nextAll("input[name='sex']").val(),
											"email":$(this).nextAll("input[name='email']").val(),
											"userType":$(this).nextAll("input[name='userType']").val(),
											"registrationTime":$(this).nextAll("input[name='registrationTime']").val(),
											"approved":result,
									};
					approvedArray.push(tmpJson); 
					console.log("approvedArray="+JSON.stringify(approvedArray));
					audit(JSON.stringify(approvedArray));
			});
			}
	});
});
